<!doctype html>
<html class="no-js" lang="en" data-content_root="../../">
  <head><meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="color-scheme" content="light dark"><link rel="index" title="Index" href="../../genindex.html"><link rel="search" title="Search" href="../../search.html">

    <!-- Generated with Sphinx 8.2.3 and Furo 2025.09.25 -->
        <title>osxphotos.photoexporter - osxphotos 0.74.2 documentation</title>
      <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=d111a655" />
    <link rel="stylesheet" type="text/css" href="../../_static/styles/furo.css?v=580074bf" />
    <link rel="stylesheet" type="text/css" href="../../_static/copybutton.css?v=76b2166b" />
    <link rel="stylesheet" type="text/css" href="../../_static/styles/furo-extensions.css?v=8dab3a3b" />
    
    


<style>
  body {
    --color-code-background: #f2f2f2;
  --color-code-foreground: #1e1e1e;
  
  }
  @media not print {
    body[data-theme="dark"] {
      --color-code-background: #202020;
  --color-code-foreground: #d0d0d0;
  
    }
    @media (prefers-color-scheme: dark) {
      body:not([data-theme="light"]) {
        --color-code-background: #202020;
  --color-code-foreground: #d0d0d0;
  
      }
    }
  }
</style></head>
  <body>
    
    <script>
      document.body.dataset.theme = localStorage.getItem("theme") || "auto";
    </script>
    

<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
  <symbol id="svg-toc" viewBox="0 0 24 24">
    <title>Contents</title>
    <svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024">
      <path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/>
    </svg>
  </symbol>
  <symbol id="svg-menu" viewBox="0 0 24 24">
    <title>Menu</title>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu">
      <line x1="3" y1="12" x2="21" y2="12"></line>
      <line x1="3" y1="6" x2="21" y2="6"></line>
      <line x1="3" y1="18" x2="21" y2="18"></line>
    </svg>
  </symbol>
  <symbol id="svg-arrow-right" viewBox="0 0 24 24">
    <title>Expand</title>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right">
      <polyline points="9 18 15 12 9 6"></polyline>
    </svg>
  </symbol>
  <symbol id="svg-sun" viewBox="0 0 24 24">
    <title>Light mode</title>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="feather-sun">
      <circle cx="12" cy="12" r="5"></circle>
      <line x1="12" y1="1" x2="12" y2="3"></line>
      <line x1="12" y1="21" x2="12" y2="23"></line>
      <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
      <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
      <line x1="1" y1="12" x2="3" y2="12"></line>
      <line x1="21" y1="12" x2="23" y2="12"></line>
      <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
      <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
    </svg>
  </symbol>
  <symbol id="svg-moon" viewBox="0 0 24 24">
    <title>Dark mode</title>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon">
      <path stroke="none" d="M0 0h24v24H0z" fill="none" />
      <path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" />
    </svg>
  </symbol>
  <symbol id="svg-sun-with-moon" viewBox="0 0 24 24">
    <title>Auto light/dark, in light mode</title>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="1" stroke-linecap="round" stroke-linejoin="round"
      class="icon-custom-derived-from-feather-sun-and-tabler-moon">
      <path style="opacity: 50%" d="M 5.411 14.504 C 5.471 14.504 5.532 14.504 5.591 14.504 C 3.639 16.319 4.383 19.569 6.931 20.352 C 7.693 20.586 8.512 20.551 9.25 20.252 C 8.023 23.207 4.056 23.725 2.11 21.184 C 0.166 18.642 1.702 14.949 4.874 14.536 C 5.051 14.512 5.231 14.5 5.411 14.5 L 5.411 14.504 Z"/>
      <line x1="14.5" y1="3.25" x2="14.5" y2="1.25"/>
      <line x1="14.5" y1="15.85" x2="14.5" y2="17.85"/>
      <line x1="10.044" y1="5.094" x2="8.63" y2="3.68"/>
      <line x1="19" y1="14.05" x2="20.414" y2="15.464"/>
      <line x1="8.2" y1="9.55" x2="6.2" y2="9.55"/>
      <line x1="20.8" y1="9.55" x2="22.8" y2="9.55"/>
      <line x1="10.044" y1="14.006" x2="8.63" y2="15.42"/>
      <line x1="19" y1="5.05" x2="20.414" y2="3.636"/>
      <circle cx="14.5" cy="9.55" r="3.6"/>
    </svg>
  </symbol>
  <symbol id="svg-moon-with-sun" viewBox="0 0 24 24">
    <title>Auto light/dark, in dark mode</title>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="1" stroke-linecap="round" stroke-linejoin="round"
      class="icon-custom-derived-from-feather-sun-and-tabler-moon">
      <path d="M 8.282 7.007 C 8.385 7.007 8.494 7.007 8.595 7.007 C 5.18 10.184 6.481 15.869 10.942 17.24 C 12.275 17.648 13.706 17.589 15 17.066 C 12.851 22.236 5.91 23.143 2.505 18.696 C -0.897 14.249 1.791 7.786 7.342 7.063 C 7.652 7.021 7.965 7 8.282 7 L 8.282 7.007 Z"/>
      <line style="opacity: 50%" x1="18" y1="3.705" x2="18" y2="2.5"/>
      <line style="opacity: 50%" x1="18" y1="11.295" x2="18" y2="12.5"/>
      <line style="opacity: 50%" x1="15.316" y1="4.816" x2="14.464" y2="3.964"/>
      <line style="opacity: 50%" x1="20.711" y1="10.212" x2="21.563" y2="11.063"/>
      <line style="opacity: 50%" x1="14.205" y1="7.5" x2="13.001" y2="7.5"/>
      <line style="opacity: 50%" x1="21.795" y1="7.5" x2="23" y2="7.5"/>
      <line style="opacity: 50%" x1="15.316" y1="10.184" x2="14.464" y2="11.036"/>
      <line style="opacity: 50%" x1="20.711" y1="4.789" x2="21.563" y2="3.937"/>
      <circle style="opacity: 50%" cx="18" cy="7.5" r="2.169"/>
    </svg>
  </symbol>
  <symbol id="svg-pencil" viewBox="0 0 24 24">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-pencil-code">
      <path d="M4 20h4l10.5 -10.5a2.828 2.828 0 1 0 -4 -4l-10.5 10.5v4" />
      <path d="M13.5 6.5l4 4" />
      <path d="M20 21l2 -2l-2 -2" />
      <path d="M17 17l-2 2l2 2" />
    </svg>
  </symbol>
  <symbol id="svg-eye" viewBox="0 0 24 24">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
      stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-eye-code">
      <path stroke="none" d="M0 0h24v24H0z" fill="none" />
      <path d="M10 12a2 2 0 1 0 4 0a2 2 0 0 0 -4 0" />
      <path
        d="M11.11 17.958c-3.209 -.307 -5.91 -2.293 -8.11 -5.958c2.4 -4 5.4 -6 9 -6c3.6 0 6.6 2 9 6c-.21 .352 -.427 .688 -.647 1.008" />
      <path d="M20 21l2 -2l-2 -2" />
      <path d="M17 17l-2 2l2 2" />
    </svg>
  </symbol>
</svg>

<input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation" aria-label="Toggle site navigation sidebar">
<input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc" aria-label="Toggle table of contents sidebar">
<label class="overlay sidebar-overlay" for="__navigation"></label>
<label class="overlay toc-overlay" for="__toc"></label>

<a class="skip-to-content muted-link" href="#furo-main-content">Skip to content</a>



<div class="page">
  <header class="mobile-header">
    <div class="header-left">
      <label class="nav-overlay-icon" for="__navigation">
        <span class="icon"><svg><use href="#svg-menu"></use></svg></span>
      </label>
    </div>
    <div class="header-center">
      <a href="../../index.html"><div class="brand">osxphotos 0.74.2 documentation</div></a>
    </div>
    <div class="header-right">
      <div class="theme-toggle-container theme-toggle-header">
        <button class="theme-toggle" aria-label="Toggle Light / Dark / Auto color theme">
          <svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg>
          <svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg>
          <svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg>
          <svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg>
        </button>
      </div>
      <label class="toc-overlay-icon toc-header-icon no-toc" for="__toc">
        <span class="icon"><svg><use href="#svg-toc"></use></svg></span>
      </label>
    </div>
  </header>
  <aside class="sidebar-drawer">
    <div class="sidebar-container">
      
      <div class="sidebar-sticky"><a class="sidebar-brand" href="../../index.html">
  
  <span class="sidebar-brand-text">osxphotos 0.74.2 documentation</span>
  
</a><form class="sidebar-search-container" method="get" action="../../search.html" role="search">
  <input class="sidebar-search" placeholder="Search" name="q" aria-label="Search">
  <input type="hidden" name="check_keywords" value="yes">
  <input type="hidden" name="area" value="default">
</form>
<div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree">
  <ul>
<li class="toctree-l1"><a class="reference internal" href="../../overview.html">OSXPhotos</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial.html">OSXPhotos Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../cli.html">OSXPhotos Command Line Interface (CLI)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../template_help.html">OSXPhotos Template System</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../package_overview.html">OSXPhotos Python Package Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../API_README.html">OSXPhotos Python API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference.html">OSXPhotos Python Reference</a></li>
</ul>

</div>
</div>

      </div>
      
    </div>
  </aside>
  <div class="main">
    <div class="content">
      <div class="article-container">
        <a href="#" class="back-to-top muted-link">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12z"></path>
          </svg>
          <span>Back to top</span>
        </a>
        <div class="content-icon-container">
          <div class="theme-toggle-container theme-toggle-content">
            <button class="theme-toggle" aria-label="Toggle Light / Dark / Auto color theme">
              <svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg>
              <svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg>
              <svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg>
              <svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg>
            </button>
          </div>
          <label class="toc-overlay-icon toc-content-icon no-toc" for="__toc">
            <span class="icon"><svg><use href="#svg-toc"></use></svg></span>
          </label>
        </div>
        <article role="main" id="furo-main-content">
          <h1>Source code for osxphotos.photoexporter</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;PhotoExport class to export photos&quot;&quot;&quot;</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">__future__</span><span class="w"> </span><span class="kn">import</span> <span class="n">annotations</span>

<span class="kn">import</span><span class="w"> </span><span class="nn">dataclasses</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">datetime</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">json</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">logging</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">os</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">pathlib</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">subprocess</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">typing</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">t</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">enum</span><span class="w"> </span><span class="kn">import</span> <span class="n">Enum</span>

<span class="kn">from</span><span class="w"> </span><span class="nn">._version</span><span class="w"> </span><span class="kn">import</span> <span class="n">__version__</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.dictdiff</span><span class="w"> </span><span class="kn">import</span> <span class="n">dictdiff</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.exif_orientation</span><span class="w"> </span><span class="kn">import</span> <span class="n">fix_exif_orientation</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.exiftool</span><span class="w"> </span><span class="kn">import</span> <span class="n">ExifTool</span><span class="p">,</span> <span class="n">exiftool_can_write</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.exifwriter</span><span class="w"> </span><span class="kn">import</span> <span class="n">ExifWriter</span><span class="p">,</span> <span class="n">exif_options_from_options</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.export_db</span><span class="w"> </span><span class="kn">import</span> <span class="n">ExportDBTemp</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.exportoptions</span><span class="w"> </span><span class="kn">import</span> <span class="n">ExportOptions</span><span class="p">,</span> <span class="n">ExportResults</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.fileutil</span><span class="w"> </span><span class="kn">import</span> <span class="n">FileUtil</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.photoinfo_common</span><span class="w"> </span><span class="kn">import</span> <span class="n">photoinfo_minify_dict</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.phototemplate</span><span class="w"> </span><span class="kn">import</span> <span class="n">RenderOptions</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.platform</span><span class="w"> </span><span class="kn">import</span> <span class="n">is_macos</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.rich_utils</span><span class="w"> </span><span class="kn">import</span> <span class="n">add_rich_markup_tag</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.sidecars</span><span class="w"> </span><span class="kn">import</span> <span class="n">SidecarWriter</span><span class="p">,</span> <span class="n">exiftool_json_sidecar</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.touch_files</span><span class="w"> </span><span class="kn">import</span> <span class="n">touch_files</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.unicode</span><span class="w"> </span><span class="kn">import</span> <span class="n">normalize_fs_path</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.uti</span><span class="w"> </span><span class="kn">import</span> <span class="n">get_preferred_uti_extension</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.utils</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
    <span class="n">increment_filename</span><span class="p">,</span>
    <span class="n">increment_filename_with_count</span><span class="p">,</span>
    <span class="n">lineno</span><span class="p">,</span>
    <span class="n">lock_filename</span><span class="p">,</span>
    <span class="n">unlock_filename</span><span class="p">,</span>
<span class="p">)</span>

<span class="k">if</span> <span class="n">is_macos</span><span class="p">:</span>
    <span class="kn">import</span><span class="w"> </span><span class="nn">photoscript</span>

    <span class="kn">from</span><span class="w"> </span><span class="nn">.photokit</span><span class="w"> </span><span class="kn">import</span> <span class="p">(</span>
        <span class="n">PHOTOS_VERSION_CURRENT</span><span class="p">,</span>
        <span class="n">PHOTOS_VERSION_ORIGINAL</span><span class="p">,</span>
        <span class="n">PHOTOS_VERSION_UNADJUSTED</span><span class="p">,</span>
        <span class="n">PhotoKitFetchFailed</span><span class="p">,</span>
        <span class="n">PhotoLibrary</span><span class="p">,</span>
    <span class="p">)</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s2">&quot;ExportError&quot;</span><span class="p">,</span>
    <span class="s2">&quot;PhotoExporter&quot;</span><span class="p">,</span>
    <span class="s2">&quot;rename_jpeg_files&quot;</span><span class="p">,</span>
<span class="p">]</span>

<span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">from</span><span class="w"> </span><span class="nn">.photoinfo</span><span class="w"> </span><span class="kn">import</span> <span class="n">PhotoInfo</span>

<span class="c1"># retry if download_missing/use_photos_export fails the first time (which sometimes it does)</span>
<span class="n">MAX_PHOTOSCRIPT_RETRIES</span> <span class="o">=</span> <span class="mi">3</span>

<span class="c1"># threshold for consecutive AppleScript export errors before restarting Photos</span>
<span class="n">APPLESCRIPT_ERROR_THRESHOLD</span> <span class="o">=</span> <span class="mi">10</span>

<span class="c1"># counter for tracking consecutive AppleScript export errors</span>
<span class="n">_consecutive_export_errors</span> <span class="o">=</span> <span class="mi">0</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s2">&quot;osxphotos&quot;</span><span class="p">)</span>


<span class="c1"># return values for _should_update_photo</span>
<span class="k">class</span><span class="w"> </span><span class="nc">ShouldUpdate</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
    <span class="n">NOT_IN_DATABASE</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">HARDLINK_DIFFERENT_FILES</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="n">NOT_HARDLINK_SAME_FILES</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="n">DEST_SIG_DIFFERENT</span> <span class="o">=</span> <span class="mi">4</span>
    <span class="n">EXPORT_OPTIONS_DIFFERENT</span> <span class="o">=</span> <span class="mi">5</span>
    <span class="n">EXIFTOOL_DIFFERENT</span> <span class="o">=</span> <span class="mi">6</span>
    <span class="n">EDITED_SIG_DIFFERENT</span> <span class="o">=</span> <span class="mi">7</span>
    <span class="n">DIGEST_DIFFERENT</span> <span class="o">=</span> <span class="mi">8</span>
    <span class="n">UPDATE_ERRORS</span> <span class="o">=</span> <span class="mi">9</span>


<span class="k">class</span><span class="w"> </span><span class="nc">ExportError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;error during export&quot;&quot;&quot;</span>

    <span class="k">pass</span>


<span class="k">class</span><span class="w"> </span><span class="nc">StagedFiles</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Represents files staged for export&quot;&quot;&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">original</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">original_live</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">edited</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">edited_live</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">preview</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">raw</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">aae</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
        <span class="n">error</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original</span> <span class="o">=</span> <span class="n">original</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original_live</span> <span class="o">=</span> <span class="n">original_live</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">edited</span> <span class="o">=</span> <span class="n">edited</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">edited_live</span> <span class="o">=</span> <span class="n">edited_live</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">preview</span> <span class="o">=</span> <span class="n">preview</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="n">raw</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">aae</span> <span class="o">=</span> <span class="n">aae</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error</span> <span class="o">=</span> <span class="n">error</span> <span class="ow">or</span> <span class="p">[]</span>

        <span class="c1"># TODO: bursts?</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__ior__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">original</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">original</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">original_live</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_live</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">original_live</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">edited</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">edited</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">edited</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">edited_live</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">edited_live</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">edited_live</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">preview</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">preview</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">preview</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw</span> <span class="ow">or</span> <span class="n">other</span><span class="o">.</span><span class="n">raw</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">error</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">asdict</span><span class="p">())</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;StagedFiles(</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">asdict</span><span class="p">()</span><span class="si">}</span><span class="s2">)&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">asdict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;original&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">original</span><span class="p">,</span>
            <span class="s2">&quot;original_live&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">original_live</span><span class="p">,</span>
            <span class="s2">&quot;edited&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">edited</span><span class="p">,</span>
            <span class="s2">&quot;edited_live&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">edited_live</span><span class="p">,</span>
            <span class="s2">&quot;preview&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">preview</span><span class="p">,</span>
            <span class="s2">&quot;raw&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">raw</span><span class="p">,</span>
            <span class="s2">&quot;error&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">error</span><span class="p">,</span>
        <span class="p">}</span>


<div class="viewcode-block" id="PhotoExporter">
<a class="viewcode-back" href="../../reference.html#osxphotos.PhotoExporter">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">PhotoExporter</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Export a photo&quot;&quot;&quot;</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span><span class="w"> </span><span class="nf">_kill_photos_process</span><span class="p">():</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Kill the Photos app process to restart it&quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># Use pkill to kill the main Photos application process</span>
            <span class="c1"># Use -f to match the full path containing Photos.app</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span>
                <span class="p">[</span><span class="s2">&quot;pkill&quot;</span><span class="p">,</span> <span class="s2">&quot;-f&quot;</span><span class="p">,</span> <span class="s2">&quot;Photos.app&quot;</span><span class="p">],</span> <span class="n">check</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">capture_output</span><span class="o">=</span><span class="kc">True</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">result</span><span class="o">.</span><span class="n">returncode</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                    <span class="s2">&quot;Photos process killed due to consecutive AppleScript failures&quot;</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;No Photos process found to kill&quot;</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Failed to kill Photos process: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">photo</span><span class="p">:</span> <span class="s2">&quot;PhotoInfo&quot;</span><span class="p">,</span> <span class="n">tmpdir</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">photo</span> <span class="o">=</span> <span class="n">photo</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span> <span class="o">=</span> <span class="n">RenderOptions</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">_verbose</span>

        <span class="c1"># define functions for adding markup</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filepath</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;filepath&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filename</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;filename&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_uuid</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_num</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;num&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="c1"># temp directory for staging downloaded missing files</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fileutil</span> <span class="o">=</span> <span class="n">FileUtil</span>

<div class="viewcode-block" id="PhotoExporter.export">
<a class="viewcode-back" href="../../reference.html#osxphotos.PhotoExporter.export">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">export</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">dest</span><span class="p">,</span>
        <span class="n">filename</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">options</span><span class="p">:</span> <span class="n">t</span><span class="o">.</span><span class="n">Optional</span><span class="p">[</span><span class="n">ExportOptions</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ExportResults</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Export photo</span>

<span class="sd">        Args:</span>
<span class="sd">            dest: must be valid destination path or exception raised</span>
<span class="sd">            filename: (optional): name of exported picture; if not provided, will use current filename</span>
<span class="sd">              **NOTE**: if provided, user must ensure file extension (suffix) is correct.</span>
<span class="sd">              For example, if photo is .CR2 file, edited image may be .jpeg.</span>
<span class="sd">              If you provide an extension different than what the actual file is,</span>
<span class="sd">              will export the photo using the incorrect file extension (unless use_photos_export is true,</span>
<span class="sd">              in which case export will use the extension provided by Photos upon export.</span>
<span class="sd">              e.g. to get the extension of the edited photo,</span>
<span class="sd">              reference PhotoInfo.path_edited</span>
<span class="sd">            options (&#39;ExportOptions&#39;): t.Optional ExportOptions instance</span>

<span class="sd">        Returns:</span>
<span class="sd">            ExportResults instance</span>

<span class="sd">        Note:</span>
<span class="sd">            To use dry run mode, you must set options.dry_run=True and also pass in memory version of export_db,</span>
<span class="sd">              and no-op fileutil (e.g. &#39;ExportDBInMemory&#39; and &#39;FileUtilNoOp&#39;) in options.export_db and options.fileutil respectively</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">options</span> <span class="o">=</span> <span class="n">options</span> <span class="ow">or</span> <span class="n">ExportOptions</span><span class="p">()</span>

        <span class="c1"># temp dir must be initialized before any of the methods called by export() are called</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_temp_dir</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>

        <span class="n">verbose</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span>
        <span class="k">if</span> <span class="n">verbose</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">callable</span><span class="p">(</span><span class="n">verbose</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;verbose must be callable&quot;</span><span class="p">)</span>

        <span class="c1"># define functions for adding markup</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filepath</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;filepath&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">rich</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filename</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;filename&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">rich</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_uuid</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">rich</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_num</span> <span class="o">=</span> <span class="n">add_rich_markup_tag</span><span class="p">(</span><span class="s2">&quot;num&quot;</span><span class="p">,</span> <span class="n">rich</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">rich</span><span class="p">)</span>

        <span class="c1"># can&#39;t use export_as_hardlink with download_missing, use_photos_export as can&#39;t hardlink the temporary files downloaded</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">download_missing</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Cannot use export_as_hardlink with download_missing or use_photos_export&quot;</span>
            <span class="p">)</span>

        <span class="c1"># when called from export(), won&#39;t get an export_db, so use temp version</span>
        <span class="n">options</span><span class="o">.</span><span class="n">export_db</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">export_db</span> <span class="ow">or</span> <span class="n">ExportDBTemp</span><span class="p">()</span>

        <span class="c1"># ensure there&#39;s a FileUtil class to use</span>
        <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span> <span class="ow">or</span> <span class="n">FileUtil</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fileutil</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">render_options</span> <span class="ow">or</span> <span class="n">RenderOptions</span><span class="p">()</span>

        <span class="c1"># export_original, and export_edited are just used for clarity in the code</span>
        <span class="n">export_original</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span>
        <span class="n">export_edited</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span>
        <span class="k">if</span> <span class="n">export_edited</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hasadjustments</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;Photo does not have adjustments, cannot export edited version&quot;</span>
            <span class="p">)</span>

        <span class="c1"># verify destination is a valid path</span>
        <span class="k">if</span> <span class="n">dest</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;dest must not be None&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">dry_run</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">dest</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">FileNotFoundError</span><span class="p">(</span><span class="s2">&quot;Invalid path passed to export&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">export_edited</span><span class="p">:</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_edited_filename</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span> <span class="o">/</span> <span class="n">filename</span>

        <span class="c1"># Is there something to convert with convert_to_jpeg?</span>
        <span class="n">dest</span><span class="p">,</span> <span class="n">options</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_convert_to_jpeg</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span>

        <span class="c1"># stage files for export by finding path in local library or downloading from iCloud as appropriate</span>
        <span class="c1"># for &#39;--download-missing&#39; and &#39;--update&#39; case, this may cause unnecessary downloads</span>
        <span class="c1"># as it will download the file even if it&#39;s not needed (won&#39;t be checked until the _should_update_photo() call from _export_photo()</span>
        <span class="c1"># fixing this will require major refactoring of the export code, see #1086</span>
        <span class="c1"># leaving it for now as this should not be a common use case</span>
        <span class="c1"># (if using &#39;--update&#39; it is much better to be using &quot;Download originals to this Mac&quot; in Photos)</span>
        <span class="n">staged_files</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage_photos_for_export</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>
        <span class="n">src</span> <span class="o">=</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">edited</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="k">else</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">original</span>

        <span class="c1"># get the right destination path depending on options.update, etc.</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_dest_path</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_render_options</span><span class="o">.</span><span class="n">filepath</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="n">all_results</span> <span class="o">=</span> <span class="n">ExportResults</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">src</span><span class="p">:</span>
            <span class="c1"># export the dest file</span>
            <span class="n">all_results</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                <span class="n">src</span><span class="p">,</span>
                <span class="n">dest</span><span class="p">,</span>
                <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">verbose</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;Skipping missing </span><span class="si">{</span><span class="s1">&#39;edited&#39;</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">options</span><span class="o">.</span><span class="n">edited</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="s1">&#39;original&#39;</span><span class="si">}</span><span class="s2"> photo </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
            <span class="p">)</span>
            <span class="n">all_results</span><span class="o">.</span><span class="n">missing</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
            <span class="n">unlock_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>

        <span class="c1"># copy live photo associated .mov if requested</span>
        <span class="k">if</span> <span class="n">export_original</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span><span class="p">:</span>
            <span class="n">live_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.mov&quot;</span>
            <span class="k">if</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">original_live</span><span class="p">:</span>
                <span class="n">src_live</span> <span class="o">=</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">original_live</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                    <span class="n">src_live</span><span class="p">,</span>
                    <span class="n">live_name</span><span class="p">,</span>
                    <span class="c1"># don&#39;t try to convert the live photo</span>
                    <span class="n">options</span><span class="o">=</span><span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">verbose</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Skipping missing live photo for </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">missing</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">live_name</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">export_edited</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span><span class="p">:</span>
            <span class="n">live_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.mov&quot;</span>
            <span class="k">if</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">edited_live</span><span class="p">:</span>
                <span class="n">src_live</span> <span class="o">=</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">edited_live</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                    <span class="n">src_live</span><span class="p">,</span>
                    <span class="n">live_name</span><span class="p">,</span>
                    <span class="c1"># don&#39;t try to convert the live photo</span>
                    <span class="n">options</span><span class="o">=</span><span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">verbose</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Skipping missing edited live photo for </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">missing</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">live_name</span><span class="p">)</span>

        <span class="c1"># copy associated RAW image if requested</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">raw_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">has_raw</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">raw</span><span class="p">:</span>
                <span class="n">raw_path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">staged_files</span><span class="o">.</span><span class="n">raw</span><span class="p">)</span>
                <span class="n">raw_ext</span> <span class="o">=</span> <span class="n">raw_path</span><span class="o">.</span><span class="n">suffix</span>
                <span class="n">raw_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">raw_ext</span><span class="si">}</span><span class="s2">&quot;</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                    <span class="n">raw_path</span><span class="p">,</span>
                    <span class="n">raw_name</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># guess at most likely raw name</span>
                <span class="n">raw_ext</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">get_preferred_uti_extension</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_raw</span><span class="p">)</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_raw</span>
                    <span class="k">else</span> <span class="s2">&quot;raw&quot;</span>
                <span class="p">)</span>
                <span class="n">raw_ext</span> <span class="o">=</span> <span class="n">raw_ext</span> <span class="ow">or</span> <span class="s2">&quot;raw&quot;</span>
                <span class="n">raw_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.</span><span class="si">{</span><span class="n">raw_ext</span><span class="si">}</span><span class="s2">&quot;</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">missing</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">raw_name</span><span class="p">)</span>
                <span class="n">verbose</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Skipping missing raw photo for </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span>

        <span class="c1"># copy preview image if requested</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">preview</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">preview</span><span class="p">:</span>
                <span class="c1"># Photos keeps multiple different derivatives and path_derivatives returns list of them</span>
                <span class="c1"># first derivative is the largest so export that one</span>
                <span class="n">preview_path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">staged_files</span><span class="o">.</span><span class="n">preview</span><span class="p">)</span>
                <span class="n">preview_ext</span> <span class="o">=</span> <span class="n">preview_path</span><span class="o">.</span><span class="n">suffix</span>
                <span class="n">preview_name</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">options</span><span class="o">.</span><span class="n">preview_suffix</span><span class="si">}{</span><span class="n">preview_ext</span><span class="si">}</span><span class="s2">&quot;</span>
                <span class="p">)</span>
                <span class="c1"># if original is missing, the filename won&#39;t have been incremented so</span>
                <span class="c1"># need to check here to make sure there aren&#39;t duplicate preview files in</span>
                <span class="c1"># the export directory</span>
                <span class="n">preview_name</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="n">preview_name</span>
                    <span class="k">if</span> <span class="nb">any</span><span class="p">([</span><span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">])</span>
                    <span class="k">else</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span>
                        <span class="n">increment_filename</span><span class="p">(</span><span class="n">preview_name</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">dry_run</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo</span><span class="p">(</span>
                    <span class="n">preview_path</span><span class="p">,</span>
                    <span class="n">preview_name</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># don&#39;t know what actual preview suffix would be but most likely jpeg</span>
                <span class="n">preview_name</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">options</span><span class="o">.</span><span class="n">preview_suffix</span><span class="si">}</span><span class="s2">.jpeg&quot;</span>
                <span class="n">all_results</span><span class="o">.</span><span class="n">missing</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">preview_name</span><span class="p">)</span>
                <span class="n">verbose</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Skipping missing preview photo for </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="n">export_original</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hasadjustments</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">export_aae</span><span class="p">:</span>
            <span class="c1"># export associated AAE adjustments file if requested but only for original images</span>
            <span class="c1"># AAE applies changes to the original so is not meaningful for the edited image</span>
            <span class="n">aae_name</span> <span class="o">=</span> <span class="n">normalize_fs_path</span><span class="p">(</span><span class="n">dest</span><span class="o">.</span><span class="n">with_suffix</span><span class="p">(</span><span class="s2">&quot;.AAE&quot;</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">staged_files</span><span class="o">.</span><span class="n">aae</span><span class="p">:</span>
                <span class="n">aae_path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">staged_files</span><span class="o">.</span><span class="n">aae</span><span class="p">)</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_aae</span><span class="p">(</span>
                    <span class="n">aae_path</span><span class="p">,</span>
                    <span class="n">aae_name</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">verbose</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Skipping adjustments for </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2">: no AAE adjustments file&quot;</span>
                <span class="p">)</span>
                <span class="n">all_results</span> <span class="o">+=</span> <span class="n">ExportResults</span><span class="p">(</span><span class="n">missing</span><span class="o">=</span><span class="p">[</span><span class="n">aae_name</span><span class="p">])</span>

        <span class="n">sidecar_writer</span> <span class="o">=</span> <span class="n">SidecarWriter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="p">)</span>
        <span class="n">all_results</span> <span class="o">+=</span> <span class="n">sidecar_writer</span><span class="o">.</span><span class="n">write_sidecar_files</span><span class="p">(</span>
            <span class="n">dest</span><span class="o">=</span><span class="n">dest</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">,</span> <span class="n">export_results</span><span class="o">=</span><span class="n">all_results</span>
        <span class="p">)</span>

        <span class="c1"># write history record for any missing files</span>
        <span class="c1"># for any exported, skipped, updated files,</span>
        <span class="c1"># the history record is written in _export_photo</span>
        <span class="c1"># but this isn&#39;t called for missing photos</span>
        <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">all_results</span><span class="o">.</span><span class="n">missing</span><span class="p">:</span>
            <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;AAE: missing&quot;</span> <span class="k">if</span> <span class="nb">str</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;.AAE&quot;</span><span class="p">)</span> <span class="k">else</span> <span class="s2">&quot;missing&quot;</span>
            <span class="n">options</span><span class="o">.</span><span class="n">export_db</span><span class="o">.</span><span class="n">set_history</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">all_results</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">_init_temp_dir</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Initialize (if necessary) the object&#39;s temporary directory.</span>

<span class="sd">        Args:</span>
<span class="sd">            options: ExportOptions object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">fileutil</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span> <span class="ow">or</span> <span class="n">FileUtil</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">tmpdir</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s2">&quot;osxphotos_export_&quot;</span><span class="p">,</span> <span class="nb">dir</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">tmpdir</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">return</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_edited_filename</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">original_filename</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return the filename for the exported edited photo</span>
<span class="sd">        (used when filename isn&#39;t provided in call to export)&quot;&quot;&quot;</span>
        <span class="c1"># need to get the right extension for edited file</span>
        <span class="n">original_filename</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">original_filename</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_edited</span><span class="p">:</span>
            <span class="n">ext</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_edited</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">uti</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_edited</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti</span>
            <span class="n">ext</span> <span class="o">=</span> <span class="n">get_preferred_uti_extension</span><span class="p">(</span><span class="n">uti</span><span class="p">)</span>
            <span class="n">ext</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;.</span><span class="si">{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="k">return</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">original_filename</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">_edited</span><span class="si">{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_get_dest_path</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">dest</span><span class="p">:</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;If destination exists find match in ExportDB, on disk, or add (1), (2), and so on to filename to get a valid destination</span>

<span class="sd">        Args:</span>
<span class="sd">            dest (str): destination path</span>
<span class="sd">            options (ExportOptions): Export options</span>

<span class="sd">        Returns:</span>
<span class="sd">            new dest path (pathlib.Path)</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># lock files are used to minimize chance of name collision when in parallel mode</span>
        <span class="c1"># don&#39;t create lock files if in dry_run mode</span>
        <span class="n">lock</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">dry_run</span>

        <span class="k">def</span><span class="w"> </span><span class="nf">_lock_filename</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
<span class="w">            </span><span class="sd">&quot;&quot;&quot;Lock filename if not in dry_run mode&quot;&quot;&quot;</span>
            <span class="k">return</span> <span class="n">lock_filename</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="k">if</span> <span class="n">lock</span> <span class="k">else</span> <span class="n">filename</span>

        <span class="c1"># if overwrite==False and #increment==False, export should fail if file exists</span>
        <span class="k">if</span> <span class="p">(</span>
            <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span>
                <span class="p">[</span>
                    <span class="n">options</span><span class="o">.</span><span class="n">increment</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">,</span>
                    <span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="p">,</span>
                <span class="p">]</span>
            <span class="p">)</span>
            <span class="ow">and</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>
        <span class="p">):</span>
            <span class="k">raise</span> <span class="ne">FileExistsError</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;destination exists (</span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">); overwrite=</span><span class="si">{</span><span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="si">}</span><span class="s2">, increment=</span><span class="si">{</span><span class="n">options</span><span class="o">.</span><span class="n">increment</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="p">)</span>

        <span class="c1"># if overwrite, we don&#39;t care if the file exists or not</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">overwrite</span> <span class="ow">and</span> <span class="n">_lock_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">dest</span>

        <span class="c1"># if not update or overwrite, check to see if file exists and if so, add (1), (2), etc</span>
        <span class="c1"># until we find one that works</span>
        <span class="c1"># Photos checks the stem and adds (1), (2), etc which avoids collision with sidecars</span>
        <span class="c1"># e.g. exporting sidecar for file1.png and file1.jpeg</span>
        <span class="c1"># if file1.png exists and exporting file1.jpeg,</span>
        <span class="c1"># dest will be file1 (1).jpeg even though file1.jpeg doesn&#39;t exist to prevent sidecar collision</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">increment</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">any</span><span class="p">(</span>
            <span class="p">[</span><span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="p">]</span>
        <span class="p">):</span>
            <span class="k">return</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">increment_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="n">lock</span><span class="p">))</span>

        <span class="c1"># if update and file exists, need to check to see if it&#39;s the right file by checking export db</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span> <span class="ow">or</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">:</span>
            <span class="n">export_db</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">export_db</span>
            <span class="n">dest_uuid</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_uuid_for_file</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">dest_uuid</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span> <span class="ow">and</span> <span class="n">_lock_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">):</span>
                <span class="c1"># destination doesn&#39;t exist in export db and doesn&#39;t exist on disk</span>
                <span class="c1"># so we can just use it</span>
                <span class="k">return</span> <span class="n">dest</span>

            <span class="k">if</span> <span class="n">dest_uuid</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">:</span>
                <span class="c1"># destination is the right file</span>
                <span class="c1"># will use it even if locked so don&#39;t check return value of _lock_filename</span>
                <span class="n">_lock_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">dest</span>

            <span class="c1"># either dest_uuid is wrong or file exists and there&#39;s no associated UUID, so find a name that matches</span>
            <span class="c1"># or create a new name if no match</span>
            <span class="c1"># find files that match &quot;dest_name (*.ext&quot; (e.g. &quot;dest_name (1).jpg&quot;, &quot;dest_name (2).jpg)&quot;, ...)</span>
            <span class="c1"># first, find all matching files in export db and see if there&#39;s a match</span>
            <span class="k">if</span> <span class="n">dest_target</span> <span class="o">:=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_target_for_file</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span> <span class="n">dest</span><span class="p">):</span>
                <span class="c1"># there&#39;s a match so use that</span>
                <span class="n">_lock_filename</span><span class="p">(</span><span class="n">dest_target</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest_target</span><span class="p">)</span>

            <span class="c1"># no match so need to create a new name</span>
            <span class="c1"># increment the destination file until we find one that doesn&#39;t exist and doesn&#39;t match another uuid in the database</span>
            <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">dest</span><span class="p">,</span> <span class="n">count</span> <span class="o">=</span> <span class="n">increment_filename_with_count</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="n">lock</span><span class="p">)</span>
            <span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">while</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_uuid_for_file</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">dest</span><span class="p">,</span> <span class="n">count</span> <span class="o">=</span> <span class="n">increment_filename_with_count</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="n">lock</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>

        <span class="c1"># fail safe...I can&#39;t think of a case that gets here</span>
        <span class="n">_lock_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">dest</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_should_update_photo</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">src</span><span class="p">:</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span> <span class="o">|</span> <span class="kc">None</span><span class="p">,</span> <span class="n">dest</span><span class="p">:</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span> <span class="o">|</span> <span class="n">ShouldUpdate</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return True if photo should be updated, else False</span>

<span class="sd">        Args:</span>
<span class="sd">            src (pathlib.Path | None): source path; if None, photo is missing and</span>
<span class="sd">                any checks that require src will return True</span>
<span class="sd">            dest (pathlib.Path): destination path</span>

<span class="sd">        Returns:</span>
<span class="sd">            False if photo should not be updated otherwise a truthy ShouldUpdate value</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># NOTE: The order of certain checks is important</span>
        <span class="c1"># read the comments below to understand why before changing</span>

        <span class="n">export_db</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">export_db</span>
        <span class="n">fileutil</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span>

        <span class="n">file_record</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">get_file_record</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">file_record</span><span class="p">:</span>
            <span class="c1"># photo doesn&#39;t exist in database, should update</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">NOT_IN_DATABASE</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">src</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">dest</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="n">src</span><span class="p">)):</span>
            <span class="c1"># different files, should update</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">HARDLINK_DIFFERENT_FILES</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">src</span> <span class="ow">or</span> <span class="n">dest</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="n">src</span><span class="p">)):</span>
            <span class="c1"># same file but not exporting as hardlink, should update</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">NOT_HARDLINK_SAME_FILES</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">ignore_signature</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span>
            <span class="n">dest</span><span class="p">,</span> <span class="n">file_record</span><span class="o">.</span><span class="n">dest_sig</span>
        <span class="p">):</span>
            <span class="c1"># destination file doesn&#39;t match what was last exported</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">DEST_SIG_DIFFERENT</span>

        <span class="k">if</span> <span class="n">file_record</span><span class="o">.</span><span class="n">export_options</span> <span class="o">!=</span> <span class="n">options</span><span class="o">.</span><span class="n">bit_flags</span><span class="p">:</span>
            <span class="c1"># exporting with different set of options (e.g. exiftool), should update</span>
            <span class="c1"># need to check this before exiftool in case exiftool options are different</span>
            <span class="c1"># and export database is missing; this will always be True if database is missing</span>
            <span class="c1"># as it&#39;ll be None and bit_flags will be an int</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">EXPORT_OPTIONS_DIFFERENT</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">update_errors</span> <span class="ow">and</span> <span class="n">file_record</span><span class="o">.</span><span class="n">error</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># files that were exported but generated an error</span>
            <span class="c1"># won&#39;t be updated unless --update-errors is specified</span>
            <span class="c1"># for example, an exiftool error due to bad metadata</span>
            <span class="c1"># that the user subsequently fixed should be updated; see #872</span>
            <span class="c1"># this must be checked before exiftool which will return False if exif data matches</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">UPDATE_ERRORS</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">exiftool</span><span class="p">:</span>
            <span class="n">current_exifdata</span> <span class="o">=</span> <span class="n">exiftool_json_sidecar</span><span class="p">(</span><span class="n">photo</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">)</span>
            <span class="n">rv</span> <span class="o">=</span> <span class="n">current_exifdata</span> <span class="o">!=</span> <span class="n">file_record</span><span class="o">.</span><span class="n">exifdata</span>
            <span class="c1"># if using exiftool, don&#39;t need to continue checking edited below</span>
            <span class="c1"># as exiftool will be used to update edited file</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">EXIFTOOL_DIFFERENT</span> <span class="k">if</span> <span class="n">rv</span> <span class="k">else</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="p">(</span>
            <span class="ow">not</span> <span class="n">src</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">cmp_file_sig</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">file_record</span><span class="o">.</span><span class="n">src_sig</span><span class="p">)</span>
        <span class="p">):</span>
            <span class="c1"># edited file in Photos doesn&#39;t match what was last exported</span>
            <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">EDITED_SIG_DIFFERENT</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">:</span>
            <span class="n">current_digest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hexdigest</span>
            <span class="k">if</span> <span class="n">current_digest</span> <span class="o">!=</span> <span class="n">file_record</span><span class="o">.</span><span class="n">digest</span><span class="p">:</span>
                <span class="c1"># metadata in Photos changed, force update</span>
                <span class="k">return</span> <span class="n">ShouldUpdate</span><span class="o">.</span><span class="n">DIGEST_DIFFERENT</span>

        <span class="c1"># photo should not be updated</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_stage_photos_for_export</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">StagedFiles</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Stages photos for export</span>

<span class="sd">        If photo is present on disk in the library, uses path to the photo on disk.</span>
<span class="sd">        If photo is missing and download_missing is true, downloads the photo from iCloud to temporary location.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">staged</span> <span class="o">=</span> <span class="n">StagedFiles</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">use_photos_export</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage_missing_photos_for_export_helper</span><span class="p">(</span><span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">raw_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">has_raw</span><span class="p">:</span>
            <span class="n">staged</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_raw</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">preview</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">:</span>
            <span class="n">staged</span><span class="o">.</span><span class="n">preview</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span><span class="p">:</span>
            <span class="c1"># original file</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path</span><span class="p">:</span>
                <span class="n">staged</span><span class="o">.</span><span class="n">original</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span><span class="p">:</span>
                <span class="n">staged</span><span class="o">.</span><span class="n">original_live</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_live_photo</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">export_aae</span><span class="p">:</span>
                <span class="n">staged</span><span class="o">.</span><span class="n">aae</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">adjustments_path</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span><span class="p">:</span>
            <span class="c1"># edited file</span>
            <span class="n">staged</span><span class="o">.</span><span class="n">edited</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_edited</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span><span class="p">:</span>
                <span class="n">staged</span><span class="o">.</span><span class="n">edited_live</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_edited_live_photo</span>

        <span class="c1"># download any missing files</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">download_missing</span><span class="p">:</span>
            <span class="n">staged</span> <span class="o">|=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage_missing_photos_for_export</span><span class="p">(</span>
                <span class="n">staged</span><span class="o">=</span><span class="n">staged</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="n">options</span>
            <span class="p">)</span>

        <span class="k">return</span> <span class="n">staged</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_stage_missing_photos_for_export</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">staged</span><span class="p">:</span> <span class="n">StagedFiles</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">StagedFiles</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Download and stage any missing files for export&quot;&quot;&quot;</span>

        <span class="c1"># if live photo and requesting edited version need the edited live photo</span>
        <span class="n">live_photo</span> <span class="o">=</span> <span class="n">staged</span><span class="o">.</span><span class="n">edited_live</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="k">else</span> <span class="n">staged</span><span class="o">.</span><span class="n">original_live</span>

        <span class="c1"># is there actually a missing file? (#1086)</span>
        <span class="n">something_to_download</span> <span class="o">=</span> <span class="p">(</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hasadjustments</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">staged</span><span class="o">.</span><span class="n">edited</span><span class="p">)</span>
            <span class="ow">or</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">live_photo</span><span class="p">)</span>
            <span class="ow">or</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">has_raw</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">raw_photo</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">staged</span><span class="o">.</span><span class="n">raw</span><span class="p">)</span>
            <span class="ow">or</span> <span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">preview</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">staged</span><span class="o">.</span><span class="n">preview</span><span class="p">)</span>
            <span class="ow">or</span> <span class="p">(</span><span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">staged</span><span class="o">.</span><span class="n">original</span><span class="p">)</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">something_to_download</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">staged</span>

        <span class="n">missing_options</span> <span class="o">=</span> <span class="n">ExportOptions</span><span class="p">(</span>
            <span class="n">edited</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">edited</span><span class="p">,</span>
            <span class="n">preview</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">preview</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">staged</span><span class="o">.</span><span class="n">preview</span><span class="p">,</span>
            <span class="n">raw_photo</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">has_raw</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">raw_photo</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">staged</span><span class="o">.</span><span class="n">raw</span><span class="p">,</span>
            <span class="n">live_photo</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">live_photo</span><span class="p">,</span>
            <span class="n">use_photokit</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">use_photokit</span><span class="p">,</span>
        <span class="p">)</span>
        <span class="n">missing_staged</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage_missing_photos_for_export_helper</span><span class="p">(</span>
            <span class="n">options</span><span class="o">=</span><span class="n">missing_options</span>
        <span class="p">)</span>
        <span class="n">staged</span> <span class="o">|=</span> <span class="n">missing_staged</span>
        <span class="k">return</span> <span class="n">staged</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_stage_missing_photos_for_export_helper</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">StagedFiles</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Stage a photo for export with AppleScript or PhotoKit&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">use_photokit</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage_photo_for_export_with_photokit</span><span class="p">(</span><span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage_photo_for_export_with_applescript</span><span class="p">(</span><span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_stage_photo_for_export_with_photokit</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">StagedFiles</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Stage a photo for export with photokit to a temporary directory&quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hasadjustments</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Edited version requested but photo has no adjustments&quot;</span><span class="p">)</span>

        <span class="n">dest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span>

        <span class="c1"># export live_photo .mov file?</span>
        <span class="n">live_photo</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span><span class="p">)</span>

        <span class="n">overwrite</span> <span class="o">=</span> <span class="nb">any</span><span class="p">([</span><span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">])</span>

        <span class="c1"># figure out which photo version to request</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">shared</span><span class="p">:</span>
            <span class="c1"># shared photos (in shared albums) show up as not having adjustments (not edited)</span>
            <span class="c1"># but Photos is unable to export the &quot;original&quot; as only a jpeg copy is shared in iCloud</span>
            <span class="c1"># so tell Photos to export the current version in this case</span>
            <span class="n">photos_version</span> <span class="o">=</span> <span class="n">PHOTOS_VERSION_CURRENT</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">has_raw</span><span class="p">:</span>
            <span class="c1"># PhotoKit always returns the raw photo of raw+jpeg pair for PHOTOS_VERSION_ORIGINAL even if JPEG is the original</span>
            <span class="n">photos_version</span> <span class="o">=</span> <span class="n">PHOTOS_VERSION_UNADJUSTED</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">photos_version</span> <span class="o">=</span> <span class="n">PHOTOS_VERSION_ORIGINAL</span>

        <span class="n">uti</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_edited</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_edited</span>
            <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti</span>
        <span class="p">)</span>
        <span class="n">ext</span> <span class="o">=</span> <span class="n">get_preferred_uti_extension</span><span class="p">(</span><span class="n">uti</span><span class="p">)</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.</span><span class="si">{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>

        <span class="n">photolib</span> <span class="o">=</span> <span class="n">PhotoLibrary</span><span class="p">()</span>
        <span class="n">results</span> <span class="o">=</span> <span class="n">StagedFiles</span><span class="p">()</span>
        <span class="n">photo</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">photo</span> <span class="o">=</span> <span class="n">photolib</span><span class="o">.</span><span class="n">fetch_uuid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">PhotoKitFetchFailed</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="c1"># if failed to find UUID, might be a burst photo</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">burst</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">_info</span><span class="p">[</span><span class="s2">&quot;burstUUID&quot;</span><span class="p">]:</span>
                <span class="n">bursts</span> <span class="o">=</span> <span class="n">photolib</span><span class="o">.</span><span class="n">fetch_burst_uuid</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">_info</span><span class="p">[</span><span class="s2">&quot;burstUUID&quot;</span><span class="p">],</span> <span class="nb">all</span><span class="o">=</span><span class="kc">True</span>
                <span class="p">)</span>
                <span class="c1"># PhotoKit UUIDs may contain &quot;/L0/001&quot; so only look at beginning</span>
                <span class="n">photo</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">bursts</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">uuid</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)]</span>
                <span class="n">photo</span> <span class="o">=</span> <span class="n">photo</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">photo</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">photo</span><span class="p">:</span>
                <span class="n">results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="p">(</span>
                        <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span>
                        <span class="sa">f</span><span class="s2">&quot;PhotoKitFetchFailed exception exporting photo </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">,</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="k">return</span> <span class="n">results</span>

        <span class="c1"># now export the requested version of the photo</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">exported</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">export</span><span class="p">(</span>
                <span class="n">dest</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
                <span class="n">dest</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                <span class="n">version</span><span class="o">=</span><span class="n">photos_version</span><span class="p">,</span>
                <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                <span class="n">video</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">results_attr</span> <span class="o">=</span> <span class="s2">&quot;edited&quot;</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="k">else</span> <span class="s2">&quot;original&quot;</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">results_attr</span><span class="p">,</span> <span class="n">exported</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">exported_file</span> <span class="ow">in</span> <span class="n">exported</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">exported_file</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;.mov&quot;</span><span class="p">):</span>
                        <span class="c1"># live photo</span>
                        <span class="n">results_attr</span> <span class="o">=</span> <span class="p">(</span>
                            <span class="s2">&quot;edited_live&quot;</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="k">else</span> <span class="s2">&quot;original_live&quot;</span>
                        <span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">results_attr</span> <span class="o">=</span> <span class="s2">&quot;edited&quot;</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="k">else</span> <span class="s2">&quot;original&quot;</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">results_attr</span><span class="p">,</span> <span class="n">exported_file</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">raw_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">has_raw</span><span class="p">:</span>
            <span class="c1"># also request the raw photo</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">exported</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">export</span><span class="p">(</span>
                    <span class="n">dest</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
                    <span class="n">dest</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
                    <span class="n">version</span><span class="o">=</span><span class="n">photos_version</span><span class="p">,</span>
                    <span class="n">raw</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
                    <span class="n">video</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="n">exported</span><span class="p">:</span>
                    <span class="n">results</span><span class="o">.</span><span class="n">raw</span> <span class="o">=</span> <span class="n">exported</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">preview</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">:</span>
            <span class="n">results</span><span class="o">.</span><span class="n">preview</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">results</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_stage_photo_for_export_with_applescript</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">StagedFiles</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Stage a photo for export with AppleScript to a temporary directory</span>

<span class="sd">        Note: If exporting an edited live photo, the associated live video will not be exported.</span>
<span class="sd">        This is a limitation of the Photos AppleScript interface and Photos behaves the same way.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hasadjustments</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Edited version requested but photo has no adjustments&quot;</span><span class="p">)</span>

        <span class="n">dest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">increment_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">))</span>

        <span class="c1"># export live_photo .mov file?</span>
        <span class="n">live_photo</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">live_photo</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">live_photo</span><span class="p">)</span>
        <span class="n">overwrite</span> <span class="o">=</span> <span class="nb">any</span><span class="p">([</span><span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">])</span>
        <span class="n">edited_version</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">shared</span><span class="p">)</span>
        <span class="c1"># shared photos (in shared albums) show up as not having adjustments (not edited)</span>
        <span class="c1"># but Photos is unable to export the &quot;original&quot; as only a jpeg copy is shared in iCloud</span>
        <span class="c1"># so tell Photos to export the current version in this case</span>
        <span class="n">uti</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_edited</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_edited</span>
            <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti</span>
        <span class="p">)</span>
        <span class="n">ext</span> <span class="o">=</span> <span class="n">get_preferred_uti_extension</span><span class="p">(</span><span class="n">uti</span><span class="p">)</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">.</span><span class="si">{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>

        <span class="n">results</span> <span class="o">=</span> <span class="n">StagedFiles</span><span class="p">()</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">exported</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_export_photo_uuid_applescript</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
                <span class="n">dest</span><span class="o">.</span><span class="n">parent</span><span class="p">,</span>
                <span class="n">filestem</span><span class="o">=</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="p">,</span>
                <span class="n">original</span><span class="o">=</span><span class="ow">not</span> <span class="n">edited_version</span><span class="p">,</span>
                <span class="n">edited</span><span class="o">=</span><span class="n">edited_version</span><span class="p">,</span>
                <span class="n">live_photo</span><span class="o">=</span><span class="n">live_photo</span><span class="p">,</span>
                <span class="n">timeout</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">timeout</span><span class="p">,</span>
                <span class="n">burst</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">burst</span><span class="p">,</span>
                <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">except</span> <span class="n">ExportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">results</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">results_attr</span> <span class="o">=</span> <span class="s2">&quot;edited&quot;</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="k">else</span> <span class="s2">&quot;original&quot;</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">results_attr</span><span class="p">,</span> <span class="n">exported</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">exported</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="c1"># could be live or raw+jpeg</span>
            <span class="k">for</span> <span class="n">exported_file</span> <span class="ow">in</span> <span class="n">exported</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">exported_file</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;.mov&quot;</span><span class="p">):</span>
                    <span class="c1"># live photo</span>
                    <span class="n">results_attr</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="s2">&quot;edited_live&quot;</span>
                        <span class="k">if</span> <span class="n">live_photo</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span>
                        <span class="k">else</span> <span class="s2">&quot;original_live&quot;</span> <span class="k">if</span> <span class="n">live_photo</span> <span class="k">else</span> <span class="kc">None</span>
                    <span class="p">)</span>
                <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">has_raw</span> <span class="ow">and</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span>
                    <span class="n">exported_file</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
                <span class="p">)</span><span class="o">.</span><span class="n">suffix</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span>
                    <span class="s2">&quot;.jpg&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;.jpeg&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;.heic&quot;</span><span class="p">,</span>
                <span class="p">]:</span>
                    <span class="c1"># assume raw photo if not a common non-raw image format</span>
                    <span class="n">results_attr</span> <span class="o">=</span> <span class="s2">&quot;raw&quot;</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">raw_photo</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">results_attr</span> <span class="o">=</span> <span class="s2">&quot;edited&quot;</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="k">else</span> <span class="s2">&quot;original&quot;</span>
                <span class="k">if</span> <span class="n">results_attr</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">results_attr</span><span class="p">,</span> <span class="n">exported_file</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">preview</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">:</span>
            <span class="n">results</span><span class="o">.</span><span class="n">preview</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">path_derivatives</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">results</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_should_convert_to_jpeg</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">dest</span><span class="p">:</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">t</span><span class="o">.</span><span class="n">Tuple</span><span class="p">[</span><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">,</span> <span class="n">ExportOptions</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Determine if a file really should be converted to jpeg or not</span>
<span class="sd">        and return the new destination and ExportOptions instance with the convert_to_jpeg flag set appropriately</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">options</span><span class="o">.</span><span class="n">convert_to_jpeg</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">isphoto</span><span class="p">):</span>
            <span class="c1"># nothing to convert</span>
            <span class="k">return</span> <span class="n">dest</span><span class="p">,</span> <span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="n">convert_to_jpeg</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">ext</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="n">options</span><span class="o">.</span><span class="n">jpeg_ext</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">jpeg_ext</span> <span class="k">else</span> <span class="s2">&quot;.jpeg&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti_original</span> <span class="o">!=</span> <span class="s2">&quot;public.jpeg&quot;</span><span class="p">:</span>
            <span class="c1"># not a jpeg but will convert to jpeg upon export so fix file extension</span>
            <span class="n">convert_to_jpeg</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">dest</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="k">elif</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uti</span> <span class="o">!=</span> <span class="s2">&quot;public.jpeg&quot;</span><span class="p">:</span>
            <span class="c1"># in Big Sur+, edited HEICs are HEIC</span>
            <span class="n">convert_to_jpeg</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">dest</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">dest</span><span class="o">.</span><span class="n">stem</span><span class="si">}{</span><span class="n">ext</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="k">return</span> <span class="n">dest</span><span class="p">,</span> <span class="n">dataclasses</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">convert_to_jpeg</span><span class="o">=</span><span class="n">convert_to_jpeg</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_is_temp_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filepath</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Returns True if file is in the PhotosExporter temp directory otherwise False&quot;&quot;&quot;</span>
        <span class="n">filepath</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">filepath</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">filepath</span><span class="o">.</span><span class="n">parent</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_copy_to_temp_file</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filepath</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Copies filepath to a temp file preserving access and modification times&quot;&quot;&quot;</span>
        <span class="n">filepath</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">filepath</span><span class="p">)</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span> <span class="o">/</span> <span class="n">filepath</span><span class="o">.</span><span class="n">name</span>
        <span class="n">dest</span> <span class="o">=</span> <span class="n">increment_filename</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fileutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">filepath</span><span class="p">,</span> <span class="n">dest</span><span class="p">)</span>
        <span class="n">stat</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">filepath</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fileutil</span><span class="o">.</span><span class="n">utime</span><span class="p">(</span><span class="n">dest</span><span class="p">,</span> <span class="p">(</span><span class="n">stat</span><span class="o">.</span><span class="n">st_atime</span><span class="p">,</span> <span class="n">stat</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_export_photo</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">src</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">dest</span><span class="p">:</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">,</span>
        <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">,</span>
    <span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Helper function for export()</span>
<span class="sd">            Does the actual copy or hardlink taking the appropriate</span>
<span class="sd">            action depending on update, overwrite, export_as_hardlink</span>
<span class="sd">            Assumes destination is the right destination (e.g. UUID matches)</span>
<span class="sd">            Sets UUID and JSON info for exported file using set_uuid_for_file, set_info_for_uuid</span>
<span class="sd">            Expects that src is a temporary file (as set by _stage_photos_for_export) and</span>
<span class="sd">            may modify the src (e.g. for convert_to_jpeg or exiftool)</span>

<span class="sd">        Args:</span>
<span class="sd">            src (str): src path</span>
<span class="sd">            dest (pathlib.Path): dest path</span>
<span class="sd">            options (ExportOptions): options for export</span>

<span class="sd">        Returns:</span>
<span class="sd">            ExportResults</span>

<span class="sd">        Raises:</span>
<span class="sd">            ValueError if export_as_hardlink and convert_to_jpeg both True</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">verbose</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">convert_to_jpeg</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;export_as_hardlink and convert_to_jpeg cannot both be True&quot;</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">export_as_hardlink</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_temp_file</span><span class="p">(</span><span class="n">src</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;export_as_hardlink cannot be used with temp files&quot;</span><span class="p">)</span>

        <span class="n">exported_files</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">update_updated_files</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">update_new_files</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">update_skipped_files</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># skip files that are already up to date</span>
        <span class="n">converted_to_jpeg_files</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">exif_results</span> <span class="o">=</span> <span class="n">ExportResults</span><span class="p">()</span>

        <span class="n">dest_str</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="n">dest_exists</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span>

        <span class="n">fileutil</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span>
        <span class="n">export_db</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">export_db</span>

        <span class="n">action</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span> <span class="ow">or</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">:</span>  <span class="c1"># updating</span>
            <span class="k">if</span> <span class="n">dest_exists</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">update_reason</span> <span class="o">:=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_update_photo</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">options</span><span class="p">):</span>
                    <span class="n">update_updated_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                    <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;update: &quot;</span> <span class="o">+</span> <span class="n">update_reason</span><span class="o">.</span><span class="n">name</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">update_skipped_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                    <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;skip&quot;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># update, destination doesn&#39;t exist (new file)</span>
                <span class="n">update_new_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
                <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;new&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># not update, export the file</span>
            <span class="n">exported_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>
            <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;export&quot;</span>

        <span class="n">export_files</span> <span class="o">=</span> <span class="n">update_new_files</span> <span class="o">+</span> <span class="n">update_updated_files</span> <span class="o">+</span> <span class="n">exported_files</span>
        <span class="k">for</span> <span class="n">export_dest</span> <span class="ow">in</span> <span class="n">export_files</span><span class="p">:</span>
            <span class="c1"># set src_sig before any modifications by convert_to_jpeg or exiftool</span>
            <span class="n">export_record</span> <span class="o">=</span> <span class="n">export_db</span><span class="o">.</span><span class="n">create_or_get_file_record</span><span class="p">(</span>
                <span class="n">export_dest</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span>
            <span class="p">)</span>
            <span class="n">export_record</span><span class="o">.</span><span class="n">src_sig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">src</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">dest_exists</span> <span class="ow">and</span> <span class="nb">any</span><span class="p">(</span>
                <span class="p">[</span><span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">]</span>
            <span class="p">):</span>
                <span class="c1"># need to remove the destination first</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;Error removing file </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> ((</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                    <span class="p">)</span> <span class="kn">from</span><span class="w"> </span><span class="nn">e</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">export_as_hardlink</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">hardlink</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;Error hardlinking </span><span class="si">{</span><span class="n">src</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                    <span class="p">)</span> <span class="kn">from</span><span class="w"> </span><span class="nn">e</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">convert_to_jpeg</span><span class="p">:</span>
                    <span class="c1"># use convert_to_jpeg to export the file</span>
                    <span class="c1"># convert to a temp file before copying</span>
                    <span class="n">tmp_file</span> <span class="o">=</span> <span class="n">increment_filename</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span>
                        <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">src</span><span class="p">)</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">_converted_to_jpeg.jpeg&quot;</span>
                    <span class="p">)</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">convert_to_jpeg</span><span class="p">(</span>
                        <span class="n">src</span><span class="p">,</span> <span class="n">tmp_file</span><span class="p">,</span> <span class="n">compression_quality</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">jpeg_quality</span>
                    <span class="p">)</span>
                    <span class="n">src</span> <span class="o">=</span> <span class="n">tmp_file</span>
                    <span class="n">converted_to_jpeg_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">exiftool</span> <span class="ow">or</span> <span class="n">options</span><span class="o">.</span><span class="n">fix_orientation</span><span class="p">:</span>
                    <span class="c1"># if exiftool or fix_orientation, write the metadata</span>
                    <span class="c1"># need to copy the file to a temp file before writing metadata</span>
                    <span class="n">src</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">src</span><span class="p">)</span>
                    <span class="n">tmp_file</span> <span class="o">=</span> <span class="n">increment_filename</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">src</span><span class="o">.</span><span class="n">stem</span><span class="si">}</span><span class="s2">_exiftool</span><span class="si">{</span><span class="n">src</span><span class="o">.</span><span class="n">suffix</span><span class="si">}</span><span class="s2">&quot;</span>
                    <span class="p">)</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">tmp_file</span><span class="p">)</span>
                    <span class="c1"># point src to the tmp_file so that the original source is not modified</span>
                    <span class="c1"># and the export grabs the new file</span>
                    <span class="n">src</span> <span class="o">=</span> <span class="n">tmp_file</span>

                    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">exiftool</span><span class="p">:</span>
                        <span class="n">exif_results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">write_exiftool_metadata_to_file</span><span class="p">(</span>
                            <span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="n">options</span>
                        <span class="p">)</span>

                    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">fix_orientation</span><span class="p">:</span>
                        <span class="n">fixed</span><span class="p">,</span> <span class="n">msg</span> <span class="o">=</span> <span class="n">fix_exif_orientation</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="p">,</span> <span class="n">src</span><span class="p">,</span> <span class="n">options</span><span class="p">)</span>
                        <span class="n">verbose</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

                <span class="k">try</span><span class="p">:</span>
                    <span class="n">fileutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest_str</span><span class="p">)</span>
                    <span class="n">verbose</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;Exported </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filepath</span><span class="p">(</span><span class="n">normalize_fs_path</span><span class="p">(</span><span class="n">dest_str</span><span class="p">))</span><span class="si">}</span><span class="s2">&quot;</span>
                    <span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;Error copying file </span><span class="si">{</span><span class="n">src</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="n">dest_str</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                    <span class="p">)</span> <span class="kn">from</span><span class="w"> </span><span class="nn">e</span>

        <span class="n">results</span> <span class="o">=</span> <span class="n">ExportResults</span><span class="p">(</span>
            <span class="n">converted_to_jpeg</span><span class="o">=</span><span class="n">converted_to_jpeg_files</span><span class="p">,</span>
            <span class="n">error</span><span class="o">=</span><span class="n">exif_results</span><span class="o">.</span><span class="n">error</span><span class="p">,</span>
            <span class="n">exif_updated</span><span class="o">=</span><span class="n">exif_results</span><span class="o">.</span><span class="n">exif_updated</span><span class="p">,</span>
            <span class="n">exiftool_error</span><span class="o">=</span><span class="n">exif_results</span><span class="o">.</span><span class="n">exiftool_error</span><span class="p">,</span>
            <span class="n">exiftool_warning</span><span class="o">=</span><span class="n">exif_results</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="p">,</span>
            <span class="n">exported</span><span class="o">=</span><span class="n">exported_files</span> <span class="o">+</span> <span class="n">update_new_files</span> <span class="o">+</span> <span class="n">update_updated_files</span><span class="p">,</span>
            <span class="n">new</span><span class="o">=</span><span class="n">update_new_files</span><span class="p">,</span>
            <span class="n">skipped</span><span class="o">=</span><span class="n">update_skipped_files</span><span class="p">,</span>
            <span class="n">updated</span><span class="o">=</span><span class="n">update_updated_files</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="c1"># touch files if needed</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">touch_file</span><span class="p">:</span>
            <span class="n">results</span> <span class="o">+=</span> <span class="n">touch_files</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="p">,</span>
                <span class="n">exported_files</span>
                <span class="o">+</span> <span class="n">update_new_files</span>
                <span class="o">+</span> <span class="n">update_updated_files</span>
                <span class="o">+</span> <span class="n">update_skipped_files</span><span class="p">,</span>
                <span class="n">options</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="c1"># set data in the database</span>
        <span class="k">with</span> <span class="n">export_db</span><span class="o">.</span><span class="n">create_or_get_file_record</span><span class="p">(</span><span class="n">dest_str</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">)</span> <span class="k">as</span> <span class="n">rec</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">rec</span><span class="o">.</span><span class="n">photoinfo</span><span class="p">:</span>
                <span class="n">last_data</span> <span class="o">=</span> <span class="n">photoinfo_minify_dict</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">rec</span><span class="o">.</span><span class="n">photoinfo</span><span class="p">))</span>
                <span class="c1"># to avoid issues with datetime comparisons, list order</span>
                <span class="c1"># need to deserialize from photo.json() instead of using photo.asdict()</span>
                <span class="n">current_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">json</span><span class="p">(</span><span class="n">shallow</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">diff</span> <span class="o">=</span> <span class="n">dictdiff</span><span class="p">(</span><span class="n">last_data</span><span class="p">,</span> <span class="n">current_data</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="c1"># just in case there&#39;s an edge case not caught by dictdiff</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;Error comparing last_data and current_data: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                    <span class="p">)</span>
                    <span class="n">diff</span> <span class="o">=</span> <span class="p">[]</span>

                <span class="k">def</span><span class="w"> </span><span class="nf">_json_default</span><span class="p">(</span><span class="n">o</span><span class="p">):</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">o</span><span class="p">,</span> <span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)):</span>
                        <span class="k">return</span> <span class="n">o</span><span class="o">.</span><span class="n">isoformat</span><span class="p">()</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">o</span><span class="p">,</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">):</span>
                        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">o</span><span class="p">)</span>

                <span class="n">diff</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">diff</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">_json_default</span><span class="p">)</span> <span class="k">if</span> <span class="n">diff</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">diff</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="n">rec</span><span class="o">.</span><span class="n">photoinfo</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">json</span><span class="p">(</span><span class="n">shallow</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">rec</span><span class="o">.</span><span class="n">export_options</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">bit_flags</span>
            <span class="c1"># don&#39;t set src_sig as that is set above before any modifications by convert_to_jpeg or exiftool</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">ignore_signature</span><span class="p">:</span>
                <span class="n">rec</span><span class="o">.</span><span class="n">dest_sig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">exiftool</span><span class="p">:</span>
                <span class="n">rec</span><span class="o">.</span><span class="n">exifdata</span> <span class="o">=</span> <span class="n">exiftool_json_sidecar</span><span class="p">(</span><span class="n">photo</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hexdigest</span> <span class="o">!=</span> <span class="n">rec</span><span class="o">.</span><span class="n">digest</span><span class="p">:</span>
                <span class="n">results</span><span class="o">.</span><span class="n">metadata_changed</span> <span class="o">=</span> <span class="p">[</span><span class="n">dest_str</span><span class="p">]</span>
            <span class="n">rec</span><span class="o">.</span><span class="n">digest</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">hexdigest</span>
            <span class="c1"># save errors to the export database (#872)</span>
            <span class="k">if</span> <span class="p">(</span>
                <span class="n">results</span><span class="o">.</span><span class="n">error</span>
                <span class="ow">or</span> <span class="n">exif_results</span><span class="o">.</span><span class="n">exiftool_error</span>
                <span class="ow">or</span> <span class="n">exif_results</span><span class="o">.</span><span class="n">exiftool_warning</span>
            <span class="p">):</span>
                <span class="n">rec</span><span class="o">.</span><span class="n">error</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s2">&quot;error&quot;</span><span class="p">:</span> <span class="n">results</span><span class="o">.</span><span class="n">error</span><span class="p">,</span>
                    <span class="s2">&quot;exiftool_error&quot;</span><span class="p">:</span> <span class="n">exif_results</span><span class="o">.</span><span class="n">exiftool_error</span><span class="p">,</span>
                    <span class="s2">&quot;exiftool_warning&quot;</span><span class="p">:</span> <span class="n">exif_results</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="p">,</span>
                <span class="p">}</span>

        <span class="n">export_db</span><span class="o">.</span><span class="n">set_history</span><span class="p">(</span>
            <span class="n">filename</span><span class="o">=</span><span class="n">dest_str</span><span class="p">,</span> <span class="n">uuid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="n">action</span><span class="p">,</span> <span class="n">diff</span><span class="o">=</span><span class="n">diff</span>
        <span class="p">)</span>

        <span class="c1"># clean up lock file</span>
        <span class="n">unlock_filename</span><span class="p">(</span><span class="n">dest_str</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">results</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_export_photo_uuid_applescript</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">uuid</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">dest</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
        <span class="n">filestem</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">original</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
        <span class="n">edited</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">live_photo</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">timeout</span><span class="o">=</span><span class="mi">120</span><span class="p">,</span>
        <span class="n">burst</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">dry_run</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
        <span class="n">overwrite</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
    <span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Export photo to dest path using applescript to control Photos</span>
<span class="sd">        If photo is a live photo, exports both the photo and associated .mov file</span>

<span class="sd">        Args:</span>
<span class="sd">            uuid: UUID of photo to export</span>
<span class="sd">            dest: destination path to export to</span>
<span class="sd">            filestem: (string) if provided, exported filename will be named stem.ext</span>
<span class="sd">                    where ext is extension of the file exported by photos (e.g. .jpeg, .mov, etc)</span>
<span class="sd">                    If not provided, file will be named with whatever name Photos uses</span>
<span class="sd">                    If filestem.ext exists, it wil be overwritten</span>
<span class="sd">            original: (boolean) if True, export original image; default = True</span>
<span class="sd">            edited: (boolean) if True, export edited photo; default = False</span>
<span class="sd">                    If photo not edited and edited=True, will still export the original image</span>
<span class="sd">                    caller must verify image has been edited</span>
<span class="sd">            *Note*: must be called with either edited or original but not both,</span>
<span class="sd">                    will raise error if called with both edited and original = True</span>
<span class="sd">            live_photo: (boolean) if True, export associated .mov live photo; default = False</span>
<span class="sd">            timeout: timeout value in seconds; export will fail if applescript run time exceeds timeout</span>
<span class="sd">            burst: (boolean) set to True if file is a burst image to avoid Photos export error</span>
<span class="sd">            dry_run: (boolean) set to True to run in &quot;dry run&quot; mode which will download file but not actually copy to destination</span>

<span class="sd">        Returns: list of paths to exported file(s) or None if export failed</span>

<span class="sd">        Raises: ExportError if error during export</span>

<span class="sd">        Note: For Live Photos, if edited=True, will export a jpeg but not the movie, even if photo</span>
<span class="sd">            has not been edited. This is due to how Photos Applescript interface works.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">global</span> <span class="n">_consecutive_export_errors</span>

        <span class="c1"># Check if we&#39;ve hit the error threshold and need to restart Photos</span>
        <span class="k">if</span> <span class="n">_consecutive_export_errors</span> <span class="o">&gt;=</span> <span class="n">APPLESCRIPT_ERROR_THRESHOLD</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;AppleScript export has failed </span><span class="si">{</span><span class="n">_consecutive_export_errors</span><span class="si">}</span><span class="s2"> consecutive times, &quot;</span>
                <span class="sa">f</span><span class="s2">&quot;restarting Photos app&quot;</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_kill_photos_process</span><span class="p">()</span>
            <span class="n">_consecutive_export_errors</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="n">dest</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">dest</span><span class="o">.</span><span class="n">is_dir</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;dest </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2"> must be a directory&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">original</span> <span class="o">^</span> <span class="n">edited</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;edited or original must be True but not both&quot;</span><span class="p">)</span>

        <span class="c1"># export to a subdirectory of tmpdir</span>
        <span class="n">tmpdir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fileutil</span><span class="o">.</span><span class="n">tmpdir</span><span class="p">(</span>
            <span class="s2">&quot;osxphotos_applescript_export_&quot;</span><span class="p">,</span> <span class="nb">dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_temp_dir_path</span>
        <span class="p">)</span>

        <span class="n">exported_files</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">filename</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># I&#39;ve seen intermittent failures with the PhotoScript export so retry if</span>
            <span class="c1"># export doesn&#39;t return anything</span>
            <span class="n">retries</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="ow">not</span> <span class="n">exported_files</span> <span class="ow">and</span> <span class="n">retries</span> <span class="o">&lt;</span> <span class="n">MAX_PHOTOSCRIPT_RETRIES</span><span class="p">:</span>
                <span class="n">photo</span> <span class="o">=</span> <span class="n">photoscript</span><span class="o">.</span><span class="n">Photo</span><span class="p">(</span><span class="n">uuid</span><span class="p">)</span>
                <span class="n">filename</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">filename</span>
                <span class="n">exported_files</span> <span class="o">=</span> <span class="n">photo</span><span class="o">.</span><span class="n">export</span><span class="p">(</span>
                    <span class="n">tmpdir</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">original</span><span class="o">=</span><span class="n">original</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span>
                <span class="p">)</span>
                <span class="n">retries</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">_consecutive_export_errors</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;AppleScript export error count: </span><span class="si">{</span><span class="n">_consecutive_export_errors</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="p">)</span>
            <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">exported_files</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">filename</span><span class="p">:</span>
            <span class="c1"># nothing got exported</span>
            <span class="n">_consecutive_export_errors</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;AppleScript export error count: </span><span class="si">{</span><span class="n">_consecutive_export_errors</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="p">)</span>
            <span class="k">raise</span> <span class="n">ExportError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Could not export photo </span><span class="si">{</span><span class="n">uuid</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">)</span>
        <span class="c1"># need to find actual filename as sometimes Photos renames JPG to jpeg on export</span>
        <span class="c1"># may be more than one file exported (e.g. if Live Photo, Photos exports both .jpeg and .mov)</span>
        <span class="c1"># TemporaryDirectory will cleanup on return</span>
        <span class="n">filename_stem</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span><span class="o">.</span><span class="n">stem</span>
        <span class="n">exported_paths</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">fname</span> <span class="ow">in</span> <span class="n">exported_files</span><span class="p">:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">tmpdir</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="o">/</span> <span class="n">fname</span>
            <span class="k">if</span> <span class="p">(</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">exported_files</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span>
                <span class="ow">and</span> <span class="ow">not</span> <span class="n">live_photo</span>
                <span class="ow">and</span> <span class="n">path</span><span class="o">.</span><span class="n">suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s2">&quot;.mov&quot;</span>
            <span class="p">):</span>
                <span class="c1"># it&#39;s the .mov part of live photo but not requested, so don&#39;t export</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">exported_files</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">burst</span> <span class="ow">and</span> <span class="n">path</span><span class="o">.</span><span class="n">stem</span> <span class="o">!=</span> <span class="n">filename_stem</span><span class="p">:</span>
                <span class="c1"># skip any burst photo that&#39;s not the one we asked for</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">filestem</span><span class="p">:</span>
                <span class="c1"># rename the file based on filestem, keeping original extension</span>
                <span class="n">dest_new</span> <span class="o">=</span> <span class="n">dest</span> <span class="o">/</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">filestem</span><span class="si">}{</span><span class="n">path</span><span class="o">.</span><span class="n">suffix</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># use the name Photos provided</span>
                <span class="n">dest_new</span> <span class="o">=</span> <span class="n">dest</span> <span class="o">/</span> <span class="n">path</span><span class="o">.</span><span class="n">name</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">dry_run</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">overwrite</span> <span class="ow">and</span> <span class="n">dest_new</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                    <span class="n">FileUtil</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">dest_new</span><span class="p">)</span>
                <span class="n">FileUtil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">path</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">dest_new</span><span class="p">))</span>
            <span class="n">exported_paths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dest_new</span><span class="p">))</span>

        <span class="c1"># Reset error counter on successful export</span>
        <span class="k">if</span> <span class="n">exported_paths</span><span class="p">:</span>
            <span class="n">_consecutive_export_errors</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">return</span> <span class="n">exported_paths</span>

    <span class="k">def</span><span class="w"> </span><span class="nf">_export_aae</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">src</span><span class="p">:</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">,</span>
        <span class="n">dest</span><span class="p">:</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">,</span>
        <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ExportResults</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Export AAE file for the photo.&quot;&quot;&quot;</span>

        <span class="c1"># AAE files describe adjustments to originals, so they don&#39;t make sense</span>
        <span class="c1"># for edited files</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">edited</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">ExportResults</span><span class="p">()</span>

        <span class="n">verbose</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span>

        <span class="n">action</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span> <span class="ow">or</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">:</span>  <span class="c1"># updating</span>
            <span class="k">if</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">update_reason</span> <span class="o">:=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_should_update_photo</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">options</span><span class="p">):</span>
                    <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;update: &quot;</span> <span class="o">+</span> <span class="n">update_reason</span><span class="o">.</span><span class="n">name</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># update_skipped_files.append(dest_str)</span>
                    <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;skip&quot;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;new&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;export&quot;</span>

        <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s2">&quot;skip&quot;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
                <span class="n">options</span><span class="o">.</span><span class="n">export_db</span><span class="o">.</span><span class="n">set_history</span><span class="p">(</span>
                    <span class="n">filename</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span>
                    <span class="n">uuid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span>
                    <span class="n">action</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;AAE: </span><span class="si">{</span><span class="n">action</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
                    <span class="n">diff</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="k">return</span> <span class="n">ExportResults</span><span class="p">(</span><span class="n">aae_skipped</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">)],</span> <span class="n">skipped</span><span class="o">=</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">)])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">action</span> <span class="o">=</span> <span class="s2">&quot;export&quot;</span>

        <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">dest</span><span class="o">.</span><span class="n">exists</span><span class="p">()</span> <span class="ow">and</span> <span class="nb">any</span><span class="p">(</span>
            <span class="p">[</span><span class="n">options</span><span class="o">.</span><span class="n">overwrite</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">]</span>
        <span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Error removing file </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> ((</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">export_as_hardlink</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span><span class="o">.</span><span class="n">hardlink</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Error hardlinking </span><span class="si">{</span><span class="n">src</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Error copying file </span><span class="si">{</span><span class="n">src</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">lineno</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="si">}</span><span class="s2">)&quot;</span>
                <span class="p">)</span>

        <span class="c1"># set data in the database</span>
        <span class="n">fileutil</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">fileutil</span>
        <span class="k">with</span> <span class="n">options</span><span class="o">.</span><span class="n">export_db</span><span class="o">.</span><span class="n">create_or_get_file_record</span><span class="p">(</span>
            <span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span>
        <span class="p">)</span> <span class="k">as</span> <span class="n">rec</span><span class="p">:</span>
            <span class="c1"># don&#39;t set src_sig as that is set above before any modifications by convert_to_jpeg or exiftool</span>
            <span class="n">rec</span><span class="o">.</span><span class="n">src_sig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">src</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">ignore_signature</span><span class="p">:</span>
                <span class="n">rec</span><span class="o">.</span><span class="n">dest_sig</span> <span class="o">=</span> <span class="n">fileutil</span><span class="o">.</span><span class="n">file_sig</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
            <span class="n">rec</span><span class="o">.</span><span class="n">export_options</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">bit_flags</span>
            <span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
                <span class="n">rec</span><span class="o">.</span><span class="n">error</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s2">&quot;error&quot;</span><span class="p">:</span> <span class="n">errors</span><span class="p">,</span>
                    <span class="s2">&quot;exiftool_error&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                    <span class="s2">&quot;exiftool_warning&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                <span class="p">}</span>

        <span class="n">options</span><span class="o">.</span><span class="n">export_db</span><span class="o">.</span><span class="n">set_history</span><span class="p">(</span>
            <span class="n">filename</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="n">uuid</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">uuid</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;AAE: </span><span class="si">{</span><span class="n">action</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">diff</span><span class="o">=</span><span class="kc">None</span>
        <span class="p">)</span>

        <span class="n">verbose</span><span class="p">(</span>
            <span class="sa">f</span><span class="s2">&quot;Exported adjustments for </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="o">.</span><span class="n">original_filename</span><span class="p">)</span><span class="si">}</span><span class="s2"> to </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filepath</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="p">)</span>

        <span class="n">written</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">)]</span>
        <span class="n">exported</span> <span class="o">=</span> <span class="n">written</span> <span class="k">if</span> <span class="n">action</span> <span class="ow">in</span> <span class="p">{</span><span class="s2">&quot;export&quot;</span><span class="p">,</span> <span class="s2">&quot;new&quot;</span><span class="p">}</span> <span class="k">else</span> <span class="p">[]</span>
        <span class="n">new</span> <span class="o">=</span> <span class="n">written</span> <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s2">&quot;new&quot;</span> <span class="k">else</span> <span class="p">[]</span>
        <span class="n">updated</span> <span class="o">=</span> <span class="n">written</span> <span class="k">if</span> <span class="s2">&quot;update&quot;</span> <span class="ow">in</span> <span class="n">action</span> <span class="k">else</span> <span class="p">[]</span>
        <span class="k">return</span> <span class="n">ExportResults</span><span class="p">(</span>
            <span class="n">aae_written</span><span class="o">=</span><span class="n">written</span><span class="p">,</span>
            <span class="n">exported</span><span class="o">=</span><span class="n">exported</span><span class="p">,</span>
            <span class="n">new</span><span class="o">=</span><span class="n">new</span><span class="p">,</span>
            <span class="n">updated</span><span class="o">=</span><span class="n">updated</span><span class="p">,</span>
            <span class="n">error</span><span class="o">=</span><span class="n">errors</span><span class="p">,</span>
        <span class="p">)</span>

<div class="viewcode-block" id="PhotoExporter.write_exiftool_metadata_to_file">
<a class="viewcode-back" href="../../reference.html#osxphotos.PhotoExporter.write_exiftool_metadata_to_file">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">write_exiftool_metadata_to_file</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">src</span><span class="p">,</span>
        <span class="n">dest</span><span class="p">,</span>
        <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">,</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">ExportResults</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Write exif metadata to src file using exiftool</span>

<span class="sd">        Caution: This method modifies *src*, not *dest*,</span>
<span class="sd">        so src must be a copy of the original file if you don&#39;t want the source modified;</span>
<span class="sd">        it also does not write to dest (dest is the intended destination for purposes of</span>
<span class="sd">        referencing the export database. This allows the exiftool update to be done on the</span>
<span class="sd">        local machine prior to being copied to the export destination which may be on a</span>
<span class="sd">        network drive or other slower external storage).&quot;&quot;&quot;</span>

        <span class="n">verbose</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_verbose</span>
        <span class="n">exiftool_results</span> <span class="o">=</span> <span class="n">ExportResults</span><span class="p">()</span>

        <span class="c1"># don&#39;t try to write if unsupported file type for exiftool</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">exiftool_can_write</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">src</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]):</span>
            <span class="n">exiftool_results</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="p">(</span>
                    <span class="n">dest</span><span class="p">,</span>
                    <span class="sa">f</span><span class="s2">&quot;Unsupported file type for exiftool, skipping exiftool for </span><span class="si">{</span><span class="n">dest</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="c1"># set file signature so the file doesn&#39;t get re-exported with --update</span>
            <span class="k">return</span> <span class="n">exiftool_results</span>

        <span class="c1"># determine if we need to write the exif metadata</span>
        <span class="c1"># if we are not updating, we always write</span>
        <span class="c1"># else, need to check the database to determine if we need to write</span>
        <span class="n">verbose</span><span class="p">(</span>
            <span class="sa">f</span><span class="s2">&quot;Writing metadata with exiftool for </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">_filepath</span><span class="p">(</span><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span><span class="o">.</span><span class="n">name</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">dry_run</span><span class="p">:</span>
            <span class="n">writer</span> <span class="o">=</span> <span class="n">ExifWriter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="p">)</span>
            <span class="n">warning_</span><span class="p">,</span> <span class="n">error_</span> <span class="o">=</span> <span class="n">writer</span><span class="o">.</span><span class="n">write_exif_data</span><span class="p">(</span>
                <span class="n">src</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="n">exif_options_from_options</span><span class="p">(</span><span class="n">options</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">warning_</span><span class="p">:</span>
                <span class="n">exiftool_results</span><span class="o">.</span><span class="n">exiftool_warning</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">warning_</span><span class="p">)))</span>
            <span class="k">if</span> <span class="n">error_</span><span class="p">:</span>
                <span class="n">exiftool_results</span><span class="o">.</span><span class="n">exiftool_error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">error_</span><span class="p">)))</span>
                <span class="n">exiftool_results</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="nb">str</span><span class="p">(</span><span class="n">dest</span><span class="p">),</span> <span class="nb">str</span><span class="p">(</span><span class="n">error_</span><span class="p">)))</span>

        <span class="n">exiftool_results</span><span class="o">.</span><span class="n">exif_updated</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="n">exiftool_results</span><span class="o">.</span><span class="n">to_touch</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">exiftool_results</span></div>


    <span class="k">def</span><span class="w"> </span><span class="nf">_should_run_exiftool</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">options</span><span class="p">:</span> <span class="n">ExportOptions</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Return True if exiftool should be run to update metadata&quot;&quot;&quot;</span>
        <span class="n">run_exiftool</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">update</span> <span class="ow">or</span> <span class="n">options</span><span class="o">.</span><span class="n">force_update</span><span class="p">:</span>
            <span class="n">files_are_different</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">exif_record</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">export_db</span><span class="o">.</span><span class="n">get_file_record</span><span class="p">(</span><span class="n">dest</span><span class="p">)</span>
            <span class="n">old_data</span> <span class="o">=</span> <span class="n">exif_record</span><span class="o">.</span><span class="n">exifdata</span> <span class="k">if</span> <span class="n">exif_record</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">old_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">old_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">old_data</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">current_data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span>
                    <span class="n">exiftool_json_sidecar</span><span class="p">(</span><span class="n">photo</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">photo</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="n">options</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="n">current_data</span> <span class="o">=</span> <span class="n">current_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">old_data</span> <span class="o">!=</span> <span class="n">current_data</span><span class="p">:</span>
                    <span class="n">files_are_different</span> <span class="o">=</span> <span class="kc">True</span>

            <span class="k">if</span> <span class="n">old_data</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">files_are_different</span><span class="p">:</span>
                <span class="c1"># didn&#39;t have old data, assume we need to write it</span>
                <span class="c1"># or files were different</span>
                <span class="n">run_exiftool</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="n">run_exiftool</span></div>



<span class="k">def</span><span class="w"> </span><span class="nf">_check_export_suffix</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">dest</span><span class="p">,</span> <span class="n">edited</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Helper function for exporting photos to check file extensions of destination path.</span>

<span class="sd">    Checks that dst file extension is appropriate for the src.</span>
<span class="sd">    If edited=True, will use src file extension of &quot;.jpeg&quot; if None provided for src.</span>

<span class="sd">    Args:</span>
<span class="sd">        src: path to source file or None.</span>
<span class="sd">        dest: path to destination file.</span>
<span class="sd">        edited: set to True if exporting an edited photo.</span>

<span class="sd">    Returns:</span>
<span class="sd">        True if src and dest extensions are OK, else False.</span>

<span class="sd">    Raises:</span>
<span class="sd">        ValueError if edited is False and src is None</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># check extension of destination</span>
    <span class="k">if</span> <span class="n">src</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># use suffix from edited file</span>
        <span class="n">actual_suffix</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">src</span><span class="p">)</span><span class="o">.</span><span class="n">suffix</span>
    <span class="k">elif</span> <span class="n">edited</span><span class="p">:</span>
        <span class="c1"># use .jpeg as that&#39;s probably correct</span>
        <span class="n">actual_suffix</span> <span class="o">=</span> <span class="s2">&quot;.jpeg&quot;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;src must not be None if edited=False&quot;</span><span class="p">)</span>

    <span class="c1"># Photo&#39;s often converts .JPG to .jpeg or .tif to .tiff on import</span>
    <span class="n">dest_ext</span> <span class="o">=</span> <span class="n">dest</span><span class="o">.</span><span class="n">suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="n">actual_ext</span> <span class="o">=</span> <span class="n">actual_suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="n">suffixes</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">([</span><span class="n">dest_ext</span><span class="p">,</span> <span class="n">actual_ext</span><span class="p">])</span>
    <span class="k">return</span> <span class="p">(</span>
        <span class="n">dest_ext</span> <span class="o">==</span> <span class="n">actual_ext</span>
        <span class="ow">or</span> <span class="n">suffixes</span> <span class="o">==</span> <span class="p">[</span><span class="s2">&quot;.jpeg&quot;</span><span class="p">,</span> <span class="s2">&quot;.jpg&quot;</span><span class="p">]</span>
        <span class="ow">or</span> <span class="n">suffixes</span> <span class="o">==</span> <span class="p">[</span><span class="s2">&quot;.tif&quot;</span><span class="p">,</span> <span class="s2">&quot;.tiff&quot;</span><span class="p">]</span>
    <span class="p">)</span>


<span class="k">def</span><span class="w"> </span><span class="nf">rename_jpeg_files</span><span class="p">(</span><span class="n">files</span><span class="p">,</span> <span class="n">jpeg_ext</span><span class="p">,</span> <span class="n">fileutil</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;rename any jpeg files in files so that extension matches jpeg_ext</span>

<span class="sd">    Args:</span>
<span class="sd">        files: list of file paths</span>
<span class="sd">        jpeg_ext: extension to use for jpeg files found in files, e.g. &quot;jpg&quot;</span>
<span class="sd">        fileutil: a FileUtil object</span>

<span class="sd">    Returns:</span>
<span class="sd">        list of files with updated names</span>

<span class="sd">    Note: If non-jpeg files found, they will be ignore and returned in the return list</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">jpeg_ext</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="n">jpeg_ext</span>
    <span class="n">jpegs</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;.jpeg&quot;</span><span class="p">,</span> <span class="s2">&quot;.jpg&quot;</span><span class="p">]</span>
    <span class="n">new_files</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">file</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">suffix</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">jpegs</span> <span class="ow">and</span> <span class="n">path</span><span class="o">.</span><span class="n">suffix</span> <span class="o">!=</span> <span class="n">jpeg_ext</span><span class="p">:</span>
            <span class="n">new_file</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="p">(</span><span class="n">path</span><span class="o">.</span><span class="n">stem</span> <span class="o">+</span> <span class="n">jpeg_ext</span><span class="p">)</span>
            <span class="n">fileutil</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">new_file</span><span class="p">)</span>
            <span class="n">new_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_file</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">new_files</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">new_files</span>
</pre></div>
        </article>
      </div>
      <footer>
        
        <div class="related-pages">
          
          
        </div>
        <div class="bottom-of-page">
          <div class="left-details">
            <div class="copyright">
                Copyright &#169; 2021, Rhet Turnbull
            </div>
            Made with <a href="https://www.sphinx-doc.org/">Sphinx</a> and <a class="muted-link" href="https://pradyunsg.me">@pradyunsg</a>'s
            
            <a href="https://github.com/pradyunsg/furo">Furo</a>
            
          </div>
          <div class="right-details">
            
          </div>
        </div>
        
      </footer>
    </div>
    <aside class="toc-drawer no-toc">
      
      
      
    </aside>
  </div>
</div><script src="../../_static/documentation_options.js?v=63568c49"></script>
    <script src="../../_static/doctools.js?v=9bcbadda"></script>
    <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../../_static/scripts/furo.js?v=46bd48cc"></script>
    <script src="../../_static/clipboard.min.js?v=a7894cd8"></script>
    <script src="../../_static/copybutton.js?v=f281be69"></script>
    </body>
</html>